/**
 * 
 */
package com.base.myproject.client.system;

import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.ui.celleditor.BaseComboBox;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * @author zgw
 *2009-7-5 上午11:13:19
 * javazgw@gmail.com
 */
public class Register {
	
	public Register()
	{
		
	}
	void init()
	{
		
	}
	public Dialog getRegDialog()
	{
		Dialog dialog = new Dialog();
		
		final FormPanel fp = new FormPanel();
		fp.setLayout(new FormLayout());
		
		LabelField note = new LabelField();
		//note.setFieldLabel("申请成功过后，需要管理员确认您的申请。");
		note.setText("申请成功过后，需要管理员确认您的申请。");
	final	TextField<String> name = new TextField<String>();
		name.setName("bname");
		name.setAllowBlank(false);
		name.setFieldLabel("姓名");
		final	TextField<String> pass = new TextField<String>();
		pass.setName("pass");
		pass.setAllowBlank(false);
		pass.setFieldLabel("密码");
		pass.setPassword(true);
	final	TextField<String> email = new TextField<String>();
		email.setName("email");
		email.setAllowBlank(false);
		email.setFieldLabel("电子邮件");
		
		
	final	ModelType combotype = new ModelType();
		combotype.setRoot("records");
		combotype.setRecordName("record");
		combotype.setTotalName("totle");
		combotype.addField("code", "bcode");
		combotype.addField("name", "bname");
		StoreInfo comboinfo = new StoreInfo();

		comboinfo.setTablename("bcode");
		comboinfo.setSql("select bcode,bname from bcode");

		final BaseComboBox<ModelData> dept = new BaseComboBox<ModelData>(
				combotype, comboinfo);
		dept.setLazyRender(false);
		dept.setFieldLabel("所属部门");
		dept.setName("dept");
		    
	/*	TextField<String> name = new TextField<String>();
		name.setName("bname");
		name.setAllowBlank(false);
		name.setFieldLabel("姓名");*/
		fp.add(note);
		fp.add(name);
		fp.add(pass);
		fp.add(email);
	
		fp.add(dept);
		dialog.add(fp);
		dialog.setLayout(new FitLayout());
		dialog.setButtons(Dialog.YESNO);
		dialog.setHideOnButtonClick(true);
		fp.setAutoHeight(true);
		fp.setWidth("500px");
		dialog.setSize(400, 300);
		
		dialog.getButtonById(Dialog.YES).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				if(!fp.isValid())
				{
					MessageBox.info("", "有必录项未填写", null);
					return ;
				}
				SendServlet send = new SendServlet();
				String str = send.getStringx("/"+GWT.getModuleName()+"/BaseServlet", "Action=regiter&"+name.getName()+"="+name.getValue()+"&"+pass.getName()+"="+pass.getValue()+"&"+email.getName()+"="+email.getValue()+"&"+dept.getName()+"="+dept.getValue().get("code"));
				Document doc = XMLParser.parse((String) str);
				  NodeList list = doc.getElementsByTagName("root");
				  Node node = list.item(0);
				  Element elem = (Element) node;
			
				if( elem.getAttribute("status").equals(Env.REGITERSUCCESS))
				{
					MessageBox.info("", "注册成功", null);
				}
				else
				{
					MessageBox.alert("", "注册失败", null);
				}
			
			}});
		return dialog;
	}

}
